import 'package:flutter/material.dart';

class GestureDetectorDemo extends StatefulWidget {
  GestureDetectorDemo({Key key}) : super(key: key);

  @override
  _GestureDetectorDemoState createState() => _GestureDetectorDemoState();
}

class _GestureDetectorDemoState extends State<GestureDetectorDemo> {
  double mx = 0, my = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('检测手势及处理事件'),
      ),
      body: FractionallySizedBox(
        widthFactor: 1,
        child: Stack(
          children: [
            GestureDetector(
              onTap: () => print('点击'),
              onDoubleTap: () => print('双击'),
              onLongPress: () => print('长按'),
              onTapDown: (e) => print('按下'),
              onTapUp: (e) => print('抬起'),
              onTapCancel: () => print('取消'),
              child: Container(
                padding: EdgeInsets.all(50),
                decoration: BoxDecoration(color: Colors.blue),
                child: Text('点我'),
              ),
            ),
            Positioned(
              left: mx,
              top: my,
              child: GestureDetector(
                onPanUpdate: (e) => _movehandle(e),
                child: Container(
                  width: 40,
                  height: 40,
                  decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.all(Radius.circular(20)),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _movehandle(DragUpdateDetails e) {
    print(e.delta.dx);
    setState(() {
      mx += e.delta.dx;
      my += e.delta.dy;
    });
  }
}
